﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MockDataTransferObject;
using System.Data.SqlClient;
using System.Data;
using MockCommon;
using System.Collections.ObjectModel;

namespace MockDataAccess
{
    public class RoleDataAccess : BaseDataAccess<Role>
    {
        private Collection<SqlParameter> _parameters;

        /// <summary>
        /// Initializes a new instance of the <see cref="RoleDataAccess"/> class.
        /// </summary>
        public RoleDataAccess()
            : base()
        {
        }

        /// <summary>
        /// Initializes a new instance of the <see cref="RoleDataAccess"/> class.
        /// </summary>
        /// <param name="isFake">if set to <c>true</c> [is fake].</param>
        public RoleDataAccess(bool isFake)
            : base(isFake)
        {
        }

        #region Get
        /// <summary>
        /// Retrieves all roles.
        /// </summary>
        /// <returns></returns>
        public DataTable RetrieveAllRoles()
        {
            DataTable table = ExecuteQueryAsDataTable(Constant.RoleGetAll, null);
            return table;
        }

        /// <summary>
        /// Gets the name of the role id by role.
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <returns></returns>
        public object GetRoleIdByRoleName(string roleName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterRoleName, Value = roleName });
            return ExecuteQueryAsScalar(Constant.RoleGetRoleIdByRoleName, _parameters);
        }

        /// <summary>
        /// Determines whether [is exist role name] [the specified role name].
        /// </summary>
        /// <param name="roleName">Name of the role.</param>
        /// <returns>
        ///   <c>true</c> if [is exist role name] [the specified role name]; otherwise, <c>false</c>.
        /// </returns>
        public bool IsExistRoleName(string roleName)
        {
            _parameters = new Collection<SqlParameter>();
            _parameters.Add(new SqlParameter { ParameterName = Constant.ParameterRoleName, Value = roleName });

            object value = ExecuteQueryAsScalar(Constant.RoleGetRoleIdByRoleName, _parameters);
            if (value == null)
            {
                return false;
            }
            return true;
        }
        #endregion
    }
}
